package com.cskaoyan.service.impl;

import cn.hutool.core.convert.Convert;
import com.cskaoyan.bean.CskaoyanmallAdmin;
import com.cskaoyan.bean.CskaoyanmallAdminExample;
import com.cskaoyan.bean.vo.AdminVo;

import com.cskaoyan.bean.vo.ZbAdminListVo;
import com.cskaoyan.mapper.CskaoyanmallAdminMapper;
import com.cskaoyan.service.AuthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    CskaoyanmallAdminMapper adminMapper;

    @Override
    public ZbAdminListVo getAdminInfo(Integer page, Integer limit, String username, String sort, String order) {
        PageHelper.startPage(page,limit);
        CskaoyanmallAdminExample adminExample = new CskaoyanmallAdminExample();
        adminExample.setOrderByClause(sort + " " + order);
        CskaoyanmallAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        List<CskaoyanmallAdmin> admins = adminMapper.selectByExample(adminExample);
        PageInfo<CskaoyanmallAdmin> adminPageInfo = new PageInfo<>(admins);
        long total = adminPageInfo.getTotal();
        ZbAdminListVo adminListVo = new ZbAdminListVo();
        ArrayList<AdminVo> adminVos = new ArrayList<>();
        for (CskaoyanmallAdmin admin:admins) {
            AdminVo adminVo = new AdminVo();
            adminVo.setId(admin.getId());
            adminVo.setUsername(admin.getUsername());
            adminVo.setAvatar(admin.getAvatar());
            Integer[] roleIds = admin.getRoleIds();
            adminVo.setRoleIds(roleIds);
            adminVos.add(adminVo);
        }
        adminListVo.setTotal(total);
        adminListVo.setItems(adminVos);
        return adminListVo;
    }

    private String encrypt(String password){
        Md5Hash md5Hash = new Md5Hash(password);
       password = md5Hash.toHex();
       return password;
    }

    @Override
    public CskaoyanmallAdmin createAdmin(Map<String, Object> map) {
        String username = (String)map.get("username");
        String password = (String)map.get("password");
        Integer[] roleIds = Convert.toIntArray(map.get("roleIds"));
        CskaoyanmallAdmin admin = new CskaoyanmallAdmin();
        admin.setUsername(username);
        admin.setRoleIds(roleIds);
        password = encrypt(password);
        admin.setPassword(password);
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        adminMapper.insertSelective(admin);
        return admin;
    }

    @Override
    public CskaoyanmallAdmin selectAdminByName(String name) {
        CskaoyanmallAdminExample example = new CskaoyanmallAdminExample();
        CskaoyanmallAdminExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(name)){
            criteria.andUsernameEqualTo(name);
        }
        CskaoyanmallAdmin admin = adminMapper.selectByName(name);
        return admin;
    }

    @Override
    public CskaoyanmallAdmin updateAdmin(CskaoyanmallAdmin admin) {
        Integer[] roleIds = Convert.toIntArray(admin.getRoleIds());
        admin.setUpdateTime(new Date());
        admin.setRoleIds(roleIds);
        adminMapper.updateByPrimaryKeySelective(admin);
        return admin;
    }

    @Override
    public void deleteAdmin(Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        CskaoyanmallAdmin admin = new CskaoyanmallAdmin();
        admin.setId(id);
        admin.setDeleted(true);
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
